/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.univcasa.view.actions;

import com.univcasa.biz.AccountDistributer;
import com.univcasa.biz.impl.AccountDistributerImpl;
import com.univcasa.biz.impl.exception.EtudiantNotFoundException;
import com.univcasa.db.model.CompteEtudiant;
import com.univcasa.db.model.Criteres;
import com.univcasa.view.forms.requestCompteStdForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author issam
 */
public class requestCompteStdAction extends Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        AccountDistributer distributor = new AccountDistributerImpl();
        requestCompteStdForm rform  = (requestCompteStdForm) form;
        String cin = rform.getCin();
        String cne = rform.getCne();
        Criteres criteria = new Criteres();
        /*@TODO
         *pas de verification du CIN tant que ne figure pas sur la BD 
         * */
        //criteria.put("cin", cin.replaceAll(" ", "").toUpperCase());
        criteria.put("cin", "%"+cin.replaceAll(" ", "").toUpperCase());
        criteria.put("cne", "%" +cne.replaceAll(" ", "").toUpperCase());
        try {
            CompteEtudiant etudiant = distributor.getAccountStdByCriteria(criteria);
            request.setAttribute("compte", etudiant );
            return mapping.findForward(SUCCESS);
        }catch (EtudiantNotFoundException exc){
            ActionErrors actionErrors = new ActionErrors();
            actionErrors.add("error", new ActionMessage("error.student.notfound") ) ;
            saveErrors(request, actionErrors);
            return mapping.findForward("failure");
        }               
    }
}
